﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmKhoDungCu : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private KhoDungCuBOCollection KhoDungCuBOCollection = null;

        private string COLUMN_FIRST_KhoDungCu = "Ten";
        private string COLUMN_SECOND_KhoDungCu = "SoThuTu";

        private string COLUMN_FIRST_MaVach = "Ten";
        private string COLUMN_SECOND_MaVach = "HeSo";
        #endregion variant

        //=====================================
        #region form
        public FrmKhoDungCu()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmKhoDungCu_Load);
            this.Shown += new EventHandler(FrmKhoDungCu_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmKhoDungCu_FormClosing);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.grdViewKhoDungCu.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            this.grdViewKhoDungCu.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmKhoDungCu_Shown(object sender, EventArgs e)
        {
            this.grdViewKhoDungCu.Focus();
        }

        private void FrmKhoDungCu_Load(object sender, EventArgs e)
        {
            RefreshData();

            Utility.InitControl(this.reLookUpDungCuID);
            Utility.InitControl(this.reLookUpDonViTinh);
        }

        private void FrmKhoDungCu_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewKhoDungCu.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }
        #endregion grid

        //=====================================
        #region function

        private void RefreshData()
        {

            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinh, donViTinhBOList, donViTinhBOList.Count);

            DungCuBOList DungCuBOList = DungCuBOList.GetDungCuBOList();
            Utility.SetData(this.reLookUpDungCuID, DungCuBOList, DungCuBOList.Count);

            #endregion BOList

            this.KhoDungCuBOCollection = KhoDungCuBOCollection.GetKhoDungCuBOCollection();

            this.bdSourceKhoDungCu.DataSource = this.KhoDungCuBOCollection.Clone();

        }

        #endregion function

    }
}